//
//  ClassifyTableViewItemCell.m
//  B2C
//
//  Created by xiaowu on 2017/9/12.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "ClassifyTableViewItemCell.h"
#import "ClassifyListCell.h"

@interface ClassifyTableViewItemCell () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *dataArr;

@property (nonatomic, strong) UIView *tableHeadView;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *headLabel;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation ClassifyTableViewItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self configureUI];
}

#pragma mark - Method
- (void)configureUI {
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView clearExtendCellLine];
    [self.tableView registerClass:[ClassifyListCell class] forCellReuseIdentifier:@"cell"];
    
    [_tableView clearExtendCellLine];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _tableView.estimatedRowHeight = 44;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;

    
}

// header
- (void)configureTableHead {
    CGFloat width = ScreenWidth - 74;
    self.tableHeadView.frame = CGRectMake(0, 0, width, 15 + 100 + 40);
    self.headImageView.frame = CGRectMake(15, 15, width - 30, 100);
    self.headLabel.frame = CGRectMake((width - self.headLabel.width) * 0.5, self.headImageView.bottom, self.headLabel.width, 40);
    self.lineView.size = CGSizeMake(45 * 2 + self.headLabel.width, 0.5);
    self.lineView.center = self.headLabel.center;
    
    [self.tableHeadView addSubview:self.headImageView];
    [self.tableHeadView addSubview:self.lineView];
    [self.tableHeadView addSubview:self.headLabel];
    
    self.tableView.tableHeaderView = self.tableHeadView;
}

- (void)setHeadLabelText:(NSString *)text WithImageUrl:(NSString *)url {
    
    NSDictionary *attributes = @{NSFontAttributeName:TextPingFangFontSize(12)};
    CGFloat width = [text boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
    
    self.headLabel.width = width + 20;
    self.headLabel.text = text;
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:PlaceholderImageBig changeContentModel:UIViewContentModeScaleAspectFill];
    
    [self configureTableHead];
}

- (void)setTableViewData:(NSArray *)arr withTitle:(NSString *)title withImageUrl:(NSString *)url {
    if (![arr isKindOfClass:[NSArray class]]) {
        return;
    }
    self.dataArr = [NSArray arrayWithArray:arr];
//    [self setHeadLabelText:title WithImageUrl:url];
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    ClassifySecondModel *secondModel = [self.dataArr objectAtIndex:section];
    return [ClassifyListCell getRowNumberWithItemArr:secondModel.threeclass];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ClassifySecondModel *secondModel = [self.dataArr objectAtIndex:indexPath.section];

    ClassifyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell setValueForCell:[ClassifyListCell getCellItemArr:secondModel.threeclass withIndexPath:indexPath] offset:74];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(NSInteger index) {
        if (weakSelf.block) {
            weakSelf.block(indexPath.section, indexPath.row * 3 + index);
        }
    };
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [ClassifyListCell getHeightWithOffset:74];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ClassifySecondModel *model = [self.dataArr objectAtIndex:section];
    
    CGFloat width = ScreenWidth - 74;
    
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor whiteColor];
    headView.frame = CGRectMake(0, 0, width, 15 + 100 + 40);
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(15, 15, width - 30, 100);
    [imageView setBackgroundColor:[UIColor imageEDBackgroundColor]];
    [imageView setContentMode:UIViewContentModeCenter];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = TextPingFangFontSize(12);
    label.textColor = [UIColor grayTextColor];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    
    
    NSDictionary *attributes = @{NSFontAttributeName:TextPingFangFontSize(12)};
    CGFloat labelWidth = [model.gtcname boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
    labelWidth = labelWidth + 20;
    label.frame = CGRectMake((width - labelWidth) * 0.5, imageView.bottom, labelWidth, 40);
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor grayTextColor];
    lineView.size = CGSizeMake(45 * 2 + labelWidth, 0.5);
    lineView.center = label.center;
    
    
    label.text = model.gtcname;
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.gtcimg] placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
    
    [headView addSubview:imageView];
    [headView addSubview:lineView];
    [headView addSubview:label];
    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 155;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] init];
    footView.backgroundColor = [UIColor whiteColor];
    return footView;
}

#pragma mark - Getters

- (UIView *)tableHeadView {
    if (!_tableHeadView) {
        _tableHeadView = [[UIView alloc] init];
    }
    return _tableHeadView;
}

- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
        [_headImageView setBackgroundColor:[UIColor imageEDBackgroundColor]];
        [_headImageView setContentMode:UIViewContentModeCenter];
    }
    return _headImageView;
}

- (UILabel *)headLabel {
    if (!_headLabel) {
        _headLabel = [[UILabel alloc] init];
        _headLabel.font = TextPingFangFontSize(12);
        _headLabel.textColor = [UIColor grayTextColor];
        _headLabel.backgroundColor = [UIColor whiteColor];
        _headLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _headLabel;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor grayTextColor];
    }
    return _lineView;
}

@end
